package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;

import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/19 22:13
 * description:
 */
public class CartServiceImpl implements CartService {
    CartDao cartDao=new CartDaoImpl();
    @Override
    public Cart findByUidAndGid(int uid, int pid) {
        return cartDao.findByUidAndGid(uid,pid);
    }

    @Override
    public void insert(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public void updateCart(Cart cart) {
        cartDao.updateCart(cart);
    }

    @Override
    public List<Cart> findCartByUid(int uid) {
        //根据uid查询购物车信息
        List<Cart> carts= cartDao.findCartByUid(uid);
        //分别查询购物车中每个商品的详情
        GoodsService goodsService=new GoodsServiceImpl();
        if(carts!=null&&carts.size()>0){
            for (Cart cart: carts) {
                //通过商品id找商品
                Goods goods=goodsService.findGoodsById(cart.getPid());
                //将商品详情添加到购物车中
                cart.setGoods(goods);
            }
        }


        return carts;
    }

    @Override
    public void deleteCart(int uid, int pid) {
        cartDao.deleteCart(uid,pid);
    }

    @Override
    public void clearCartByUid(int uid) {
        cartDao.clearCartByUid(uid);
    }
}
